<?php
if (! isset ( $_GET ['token'] )) {
	exit ();
}
$token = rawurldecode ( $_GET ['token'] );
$token = base64_decode ( $token );
$token = decrypt_password ( $token );
$token = json_decode ( $token, true );
$lience = $token ['liences'];

//连接到本地mysql数据库
$myconn = mysql_connect ( "sqld.duapp.com:4050", "h0jwoYue6kaQRVdFoUWk597T", "dz0O4WT2GwB4v1pSU5ZH9ytRbpeq6qIT" );
//选择test为操作库
mysql_query ( "set names 'utf-8'" ); // //这就是指定数据库字符集，一般放在连接数据库后面就系了
mysql_select_db ( "uHEBQmHrKDDsmsquZAmT", $myconn );
$strSql = "select * from code_token where `liences`='" . $lience . "'";
//用mysql_query函数从user表里读取数据
$result = mysql_query ( $strSql, $myconn );

$code = 403;
$statu = 0;
while ( $row = mysql_fetch_array ( $result ) ) //通过循环读取数据内容
{
	$lience2 = $row ['liences'];
	if ($lience == $lience2 && $row ['statu'] < 5) { //有未使用的liences
		$code = 200;
		$statu = $row ['statu'];
		break;
	}
}

if ($code == 200) {
	$statu = $statu + 1;
	$strSql = "update code_token set statu=".$statu." where `liences`='" . $lience . "'";
	$result = mysql_query ( $strSql, $myconn );
}
// 关闭连接
mysql_close ( $myconn );
if ($code == 200) {
	$rs = array ('code' => 200, 'time' => time () );
	echo base64_encode ( encrypt_password ( json_encode($rs,true) ) );
	exit ();
} else {
	$rs = array ('code' => 403, 'time' => time () );
	echo base64_encode ( encrypt_password ( json_encode($rs,true) ) );
	exit ();
}

/**
 * 解密密码 tanxy
 */
function decrypt_password($token) {
	$key = '!@#$%^&*';
	$result = decrypt ( $token, $key );
	return $result;
}
/**
 * 加密密码 tanxy
 * Enter description here ...
 * @param  $encrypted
 */
function encrypt_password($token) {
	$key = '!@#$%^&*';
	$result = encrypt ( $token, $key );
	return $result;
}
/**
 * 加密 tanxy
 * @param $input
 * @param $key
 */
function encrypt($input, $key) {
	$size = mcrypt_get_block_size ( 'des', 'ecb' );
	$input = pkcs5_pad ( $input, $size );
	$td = mcrypt_module_open ( 'des', '', 'ecb', '' );
	$iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
	@mcrypt_generic_init ( $td, $key, $iv );
	$data = mcrypt_generic ( $td, $input );
	mcrypt_generic_deinit ( $td );
	mcrypt_module_close ( $td );
	$data = base64_encode ( $data );
	return $data;
}
/**
 * 解密 tanxy
 *
 * @param $encrypted
 * @param $key
 */
function decrypt($encrypted, $key) {
	
	$encrypted = base64_decode ( $encrypted );
	$td = mcrypt_module_open ( 'des', '', 'ecb', '' );
	$iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
	$ks = mcrypt_enc_get_key_size ( $td );
	@mcrypt_generic_init ( $td, $key, $iv );
	$decrypted = mdecrypt_generic ( $td, $encrypted );
	mcrypt_generic_deinit ( $td );
	mcrypt_module_close ( $td );
	$y = pkcs5_unpad ( $decrypted );
	return $y;
}
function pkcs5_pad($text, $blocksize) {
	$pad = $blocksize - (strlen ( $text ) % $blocksize);
	return $text . str_repeat ( chr ( $pad ), $pad );
}
/**
 * tanxy
 * Enter description here ...
 * @param unknown_type $text
 */
function pkcs5_unpad($text) {
	$pad = ord ( $text {strlen ( $text ) - 1} );
	if ($pad > strlen ( $text ))
		return false;
	if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
		return false;
	return substr ( $text, 0, - 1 * $pad );
}

?>